import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, Platform, ToastController} from 'ionic-angular';
import $ from "jquery";

import {MessageType} from '../../../model/pages/information/message-overview/message-overview.model';
import {SideMenuService} from "../../../services/common/side-menu.service";
import {TabsPage} from "../../tabs/tabs";
import {LocalStorageService} from "../../../services/native/local-storage/local-storage.service";
import {MessageOverviewProvider} from '../../../providers/information/message-overview/message-overview.provider';
import {ToastService} from '../../../services/common/toast.service';
import {LoadingService} from "../../../services/common/loading.service";

@IonicPage()
@Component({
    selector: 'page-message-overview',
    templateUrl: 'message-overview.html',
    providers: [
        SideMenuService,
        LocalStorageService,
        MessageOverviewProvider,
        ToastService,
        LoadingService
    ]
})
export class MessageOverviewPage {
    // 消息类型
    messageType = MessageType;

    // 返回一级页面
    openRoot = (name, index) => this.openTab(name, index);
    openMenu(myEvent) {
        this.sideMenuService.openSideMenu(myEvent)
    }
    openTab(name, index) {
        this.navCtrl.popToRoot();
        this.tabsPage.openPage({name: name, index: index});
    }
    // 返回键标记
    triggerTime:number=0;
    // 预览数据
    overviewData:any[]=[];
    // 查询关键字
    private keyword:string;
    // 全量id
    private allId:string[]=[];
    // 请求状态
    private requestStatus:boolean;

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                public tabsPage: TabsPage,
                public platform:Platform,
                public toastCtrl:ToastController,
                private sideMenuService: SideMenuService,
                private storage:LocalStorageService,
                private messageOverviewProvider:MessageOverviewProvider,
                private toastService:ToastService,
                private loadingService:LoadingService
                ) {
    }
    //public backButtonHandle = () => this.backClickHandle();

    ionViewDidEnter(){
        this.getMessageType(null,1);
    }

    // 返回
    backClickHandle() {
        //this.navCtrl && this.navCtrl.pop(null, null);
        this.navCtrl.popToRoot();
    }


    // 获取消息类型列表
    getMessageType(refresher,loading){
        loading?this.loadingService.showLoading():null;
        this.messageOverviewProvider.getMsgTypeList().subscribe((res)=>{
            this.requestStatus=true;
            refresher?refresher.complete():null;
            if(res.length>0){
                this.overviewData=res;
                for(let m in this.overviewData){
                    this.allId.push(this.overviewData[m].id);
                }
                //console.log(this.allId)
            }
            loading?this.loadingService.dismissLoading():null;
        },error=>{
            loading?this.loadingService.dismissLoading():null;
            this.requestStatus=true;
        })
    }

    // 下拉刷新
    doRefresh(refresher){
        this.getMessageType(refresher,0);
    }

    // 点击全量查询
    searchHandle(){
        this.goToList(this.keyword,[],true,false,null)
    }

    // enter全量查询
    inputKeyUp(e){
        if ('Enter' === e.key || 13 === e.keyCode){
            this.goToList(this.keyword,[],true,false,null)
        }
    }

    // 跳转至消息列表
    goToList(keyword,type,search,status,title) {
        if(this.allId.length<=0){
            this.toastService.showToast('没有数据');
            return false;
        }
        this.navCtrl.push('MessageListPage',{
            keyword:keyword,
            type:type,
            search:search,
            title:title
        },{
            animate:status
        });
    }

    // 到收藏列表
    goToCollectionList () {
        this.navCtrl.push('CollectionListPage',{
            type:1
        });
    }
}
